package com.john.blog.controller.front;

import java.util.Date;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.github.pagehelper.PageInfo;
import com.john.blog.common.constant.KeyConst;
import com.john.blog.common.utils.DetectUtils;
import com.john.blog.common.utils.KeyUtils;
import com.john.blog.common.utils.RequestUtils;
import com.john.blog.common.vo.CommonResult;
import com.john.blog.model.po.Blog;
import com.john.blog.model.po.Comment;
import com.john.blog.model.po.Model;
import com.john.blog.service.BlogService;
import com.john.blog.service.CommentService;

import tk.mybatis.mapper.entity.Example;

/**
 * 评论
 * 
 * @author JiangGengchao
 * @date 2018年2月5日
 */
@RestController
@RequestMapping("/blog/comment")
public class FrontCommentController {

    @Autowired
    private CommentService commentService;
    @Autowired
    private BlogService blogService;

    /**
     * 显示评论
     */
    @RequestMapping("/show")
    public CommonResult show(@RequestParam(value = Model.ID) String blogId, @RequestParam(value = KeyConst.PAGE_NUM) Integer pageNum, @RequestParam(value = KeyConst.PAGE_SIZE) Integer pageSize) {
        Example example = new Example(Comment.class);
        example.createCriteria().andEqualTo("blogID", blogId);
        example.orderBy("createDate").desc();
        example.orderBy("likeNum").desc();
        example.orderBy("shareNum").desc();
        PageInfo<Comment> commentPageInfo = commentService.findPagination(example, pageNum, pageSize);
        return CommonResult.build(CommonResult.Code.SUCCESS, null, commentPageInfo);
    }

    @RequestMapping("/like")
    public CommonResult like(@RequestParam(value = Model.ID) String id, @RequestParam(value = "ok") String ok) {
        if (id == null) {
            return CommonResult.build(CommonResult.Code.FAILURE, "缺少评论信息", null);
        } else {
            Boolean isOk = RequestUtils.toBoolean(ok);
            Example example = new Example(Comment.class);
            example.createCriteria().andEqualTo(Model.ID, id);
            List<Comment> comments = commentService.selectByExample(example);
            if (null != comments && comments.size() == 1) {
                Comment comment = comments.get(0);
                int num = comment.getLikeNum();
                if (isOk) {
                    // 取消赞
                    comment.setLikeNum(num - 1);
                    if (commentService.updateByPrimaryKeySelective(comment) != null) {
                        return CommonResult.build(CommonResult.Code.SUCCESS, "取消赞成功", null);
                    } else {
                        return CommonResult.build(CommonResult.Code.SUCCESS, "取消赞成功", null);
                    }
                } else {
                    // 点赞
                    comment.setLikeNum(num + 1);
                    if (commentService.updateByPrimaryKeySelective(comment) != null) {
                        return CommonResult.build(CommonResult.Code.SUCCESS, "点赞成功", null);
                    } else {
                        return CommonResult.build(CommonResult.Code.SUCCESS, "点赞失败", null);
                    }
                }
            } else {
                return CommonResult.build(CommonResult.Code.FAILURE, "评论没有找到或不唯一", null);
            }
        }
    }

    @RequestMapping("/hate")
    public CommonResult hate(@RequestParam(value = Model.ID) String id, @RequestParam(value = "ok") String ok) {
        if (id == null) {
            return CommonResult.build(CommonResult.Code.FAILURE, "缺少评论信息", null);
        } else {
            Boolean isOk = RequestUtils.toBoolean(ok);
            Example example = new Example(Comment.class);
            example.createCriteria().andEqualTo(Model.ID, id);
            List<Comment> comments = commentService.selectByExample(example);
            if (null != comments && comments.size() == 1) {
                Comment comment = comments.get(0);
                int num = comment.getHateNum();
                if (isOk) {
                    // 取消赞
                    comment.setHateNum(num - 1);
                    if (commentService.updateByPrimaryKeySelective(comment) != null) {
                        return CommonResult.build(CommonResult.Code.SUCCESS, "取消踩成功", null);
                    } else {
                        return CommonResult.build(CommonResult.Code.SUCCESS, "取消踩失败", null);
                    }
                } else {
                    // 点赞
                    comment.setHateNum(num + 1);
                    if (commentService.updateByPrimaryKeySelective(comment) != null) {
                        return CommonResult.build(CommonResult.Code.SUCCESS, "踩成功", null);
                    } else {
                        return CommonResult.build(CommonResult.Code.SUCCESS, "踩失败", null);
                    }
                }
            } else {
                return CommonResult.build(CommonResult.Code.FAILURE, "评论没有找到或不唯一", null);
            }
        }
    }

    /**
     * 发表评论
     */
    @RequestMapping("/report")
    public CommonResult report(Comment comment) {
        if (DetectUtils.isEmpty(comment.getBlogID())) {
            return CommonResult.build(CommonResult.Code.FAILURE, "评论所属ID不能为空", null);
        }
        if (DetectUtils.isEmpty(comment.getContent())) {
            return CommonResult.build(CommonResult.Code.FAILURE, "评论内容不能为空", null);
        }
        if (DetectUtils.isEmpty(comment.getEmail())) {
            return CommonResult.build(CommonResult.Code.FAILURE, "邮箱不能为空", null);
        }
        if (DetectUtils.isEmpty(comment.getName())) {
            return CommonResult.build(CommonResult.Code.FAILURE, "昵称不能为空", null);
        }
        comment.setId(KeyUtils.getUUID());
        comment.setLikeNum(0);
        comment.setHateNum(0);
        comment.setReplyNum(0);
        comment.setShareNum(0);
        // 根据邮箱设置头像
        if (KeyConst.AUTHOR_EMAIL.equals(comment.getEmail())) {
            comment.setHead("author.jpg");
        } else {
            int code = comment.getEmail().hashCode();
            code = Math.abs(code) % KeyConst.HEAD_MOD;
            comment.setHead(code + ".gif");
        }
        Date date = new Date();
        comment.setCreateDate(date);
        comment.setUpdateDate(date);

        // 更新blog评论数量
        if (commentService.insert(comment) == 1) {
            if (!KeyConst.SPCMT_DNT.equals(comment.getBlogID()) && !KeyConst.SPCMT_MSG.equals(comment.getBlogID())) {
                Example example = new Example(Blog.class);
                example.createCriteria().andEqualTo(Model.ID, comment.getBlogID());
                List<Blog> blogs = blogService.selectByExample(example);
                Blog blog = blogs.get(0);
                blog.setCommentNum(blog.getCommentNum() + 1);
                blogService.updateByPrimaryKeySelective(blog);
            }
            return CommonResult.build(CommonResult.Code.SUCCESS, "发表成功", null);
        } else {
            return CommonResult.build(CommonResult.Code.SUCCESS, "发表失败", null);
        }
    }
}
